package com.lqz.tools;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.filechooser.FileSystemView;

public class Potplay {

	public static String  writeDPL(ArrayList<PotModel> exportList) {
		FileSystemView fsv = FileSystemView.getFileSystemView();
		//File com = fsv.getHomeDirectory();// 这便是读取桌面路径的方法了
		File outFile=new File(fsv.getHomeDirectory().getPath()+"/aviliable.dpl");
		if(outFile.exists()){
			outFile.delete();
		}
		try {
			outFile.createNewFile();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		BufferedWriter bw=null;
		try {
			bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));
			bw.write("DAUMPLAYLIST"+"\r\n"+"topindex=0"+"\r\n");
			for(PotModel p:exportList){
				bw.write(p.getPot());
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				bw.flush();
				bw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		return outFile.getPath();
		
	}

	public static List<PotModel> readDPL(String filePath) {
		File file = new File(filePath);
		String encoding = "UTF8";
		List<PotModel> potList = new ArrayList<PotModel>();
		if (file.isFile() && file.exists()) {

			try {
				try {
					InputStreamReader ir = new InputStreamReader(new FileInputStream(file), encoding);
					BufferedReader br = new BufferedReader(ir);
					String lineText = null;
					String allText = "";
					try {
						while ((lineText = br.readLine()) != null) {
							if (lineText.matches(("^\\d.*"))) {
								allText = allText + lineText + " ";
							}

						}

						// System.out.println(allText);
						String regex = "(\\d+)\\*file.*\\s+\\1\\*played\\*0";
						Pattern p = Pattern.compile(regex);
						Matcher m = p.matcher(allText);
						String re = "file\\*.*(http\\S*)\\s{0,1}(\\d*\\*title\\*(.*)\\s){0,}";
						Pattern p1 = Pattern.compile(re);
						int count = 0;
						while (m.find()) {
							count++;
							String s = m.group();
							Matcher m1 = p1.matcher(s);
							while (m1.find()) {
								PotModel potModel = new PotModel();
								potModel.setNum(count);
								potModel.setFile(m1.group(1));
								potModel.setTitle(m1.group(3));
								// System.out.println(m1.group(1));
								potList.add(potModel);
							}
						}
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					try {
						br.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					try {
						ir.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// System.out.println(potList.size());
		}

		return potList;
	}

	public static int isVail(String url, String param) {
		// 创建url
		URL realurl = null;
		if (param != null) {
			try {
				realurl = new URL(url + "?" + param);
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			try {
				realurl = new URL(url);
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		// 打开连接
		HttpURLConnection connection = null;
		try {
			connection = (HttpURLConnection) realurl.openConnection();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 设置通用的请求属�?
		connection.setRequestProperty("accept", "*/*");
		connection.setRequestProperty("connection", "Keep-Alive");
		connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
		// 建立连接
		try {
			connection.connect();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println(url + "-->连接超时");
			// System.out.println("连接超时");
			// e.printStackTrace();
		}
		// 获取所有有响应头字段
//		Map<String, List<String>> map = connection.getHeaderFields();
//		// 遍历�?有的响应头字段，获取到cookies�?
//		for (String key : map.keySet()) {
//			System.out.println(key + "--->" + map.get(key));
//		}
		try {
			//System.out.println("状态码" + connection.getResponseCode());
			return connection.getResponseCode();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println(url + "-->获取状态超时");
			// e.printStackTrace();
		}
		return 0;
	}
}
